package remoteControlSpy;

import java.io.File;
import java.io.IOException;

/**
 * this class is the class that handles the service
 */
public class MyService 
{

	// static instance of the service class
	private static MyService serviceInstance = new MyService();
	// flag - if service instance has stopped
	private boolean stopped = false; 

	/**
	 * the method from which tomcat will start/stop the service 
	 * @param args - like main
	 */
	public static void windowsService(String args[]) 
	{
		String cmd = "start";
		// the case that this function had input
		if(args.length > 0) cmd = args[0];

		// the case that the service need to be started
		if("start".equals(cmd))  serviceInstance.start();
		// the case that the service needs to be stopped
		else
		{
			serviceInstance.stop();
		}
	}
	
	/**
	 * starts the service instance
	 */
	public void start() 
	{
		stopped = false;
		String s = "C:/Program Files/m&l_dropbox/details.xml";
		File f = new File(s);
		// the case that the user installed the program
		if(f.exists())
		{
			StartExecuter se = StartExecuter.getInstance();
			try
			{
				// start the OS listener and updates from server
				se.execute();
			} 
			catch (IOException e) {}
		}
		// the case that the user didn't install the program 
		else
		{
			stopped = true;
			System.out.println("you havn't register yet");
		}
		
		// the service runs until it will be stopped
		while(!stopped) {}
	}

	/**
	 * Stops this service instance
	 */
	public void stop()
	{
		StopExecuter ste = StopExecuter.getInstance();
		try 
		{
			// stops the OS listener and updates from server
			ste.execute();
		} 
		catch (IOException e) {}
		stopped = true;
	}
}
